package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {
    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Override
    @Transactional
    public void saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) {
            systemDictionaryItemMapper.insert(systemDictionaryItem);
        } else {
            systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if(id != null){
            systemDictionaryItemMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SystemDictionaryItem selectOne(Long id) {
        SystemDictionaryItem systemDictionaryItem = systemDictionaryItemMapper.selectByPrimaryKey(id);
        return systemDictionaryItem;
    }

    @Override
    public List<SystemDictionaryItem> selectAll() {
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectAll();
        return systemDictionaryItems;
    }

    @Override
    public PageInfo<SystemDictionaryItem> query(QueryObject qo) {
        //开始分页,会拦截下一个查询的sql
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectPage(qo);
        return new PageInfo<>(systemDictionaryItems);
    }

    @Override
    public List<SystemDictionaryItem> selectByParentId(Long id) {
        return systemDictionaryItemMapper.selectByParentId(id);
    }

    @Override
    public List<SystemDictionaryItem> selectByTypeSn(String sn) {
        return systemDictionaryItemMapper.selectByTypeSn(sn);
    }

    @Override
    public List<SystemDictionaryItem> selectParentId(Long id) {
        return systemDictionaryItemMapper.selectParentId(id);
    }
}
